package com.zff.controller;


import com.alibaba.fastjson.JSONObject;
import com.zff.dao.XstWxUserDao;
import com.zff.entity.XstWxUser;
import com.zff.service.XstWxUserService;
import com.zff.tool.WeChatUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zff
 * @since 2020-07-31
 */


//@Controller
@CrossOrigin
@RestController
@ApiOperation("微信用户控制器")
@RequestMapping("/xstWxUser")
public class XstWxUserController {
    @Autowired
    private XstWxUserService xstWxUserService;
    @Autowired
    private XstWxUserDao xstWxUserDao;
    @Autowired
    RedisTemplate<String, Object> redisTemplate;



    @RequestMapping(value ="getUserInfo")
    @ApiOperation("获取微信用户")
    public String person(String code){
        System.out.println("code:"+code);
        List<XstWxUser> userByJson = new ArrayList<>();
        Integer userId = null;
        String openid = "";
        if(code!=null) {
            JSONObject json = WeChatUtil.getWebAccesstokenUrl(code); //通过code来换取access_token
            String webAccessToken = json.getString("access_token");//获取网页授权access_token凭据
            if(webAccessToken!=null){
                openid = json.getString("openid"); //获取用户openid
                JSONObject userInfo = WeChatUtil.getUserInfo(webAccessToken, openid);            //通过access_token和openid拉取用户信息
                userId = xstWxUserService.getUserByJson(userInfo);
            }
        }
        String url_to = "http://xspx.zhyww.cn/Show?openid=" + openid;
        return openid;
    }


    /**
     * 完善用户信息
     * @param form
     * @return
     */
    @PostMapping("/updateUser")
    public Map updateUser(@RequestBody JSONObject form){
        HashMap<String, Object> map = new HashMap<>();
        System.out.println("jjajajaj:"+form);
        Integer integer = xstWxUserService.updateUser(form);
        if (integer == 1){
            map.put("states", 1);
            map.put("msg", "完善信息成功");
        } else {
            map.put("states", 0);
            map.put("msg", "完善信息失败");
        }
        return map;
    }



    /**
     * 获取isVoice（声明）
     * @param userId
     * @return
     */
    @GetMapping("/getIsVoice")
    public  String getIsVoice(String userId){
        String isVoice = xstWxUserService.getIsVoice(userId);
        return isVoice;
    }

    /**
     * 获取用户信息
     * @param openid
     * @return
     */
    @GetMapping("/getUser")
    public List<XstWxUser>  getUser(String openid){
        List<XstWxUser> xstUser = xstWxUserService.getXstUser(openid);
        return xstUser;
    }

    /**
     * 用户确认声明
     * @param userId
     * @return
     */
    @GetMapping("/updateIsVoice")
    public Integer updateIsVoice(Integer userId,String openid){
        XstWxUser xstWxUser = new XstWxUser();
        xstWxUser.setId(userId);
        xstWxUser.setIsVoice("1");
        int count = xstWxUserDao.updateById(xstWxUser);
        HashMap<String, Object> map = new HashMap<>();
        map.put("openid",openid);
        List<XstWxUser> xstWxUsers = xstWxUserDao.selectByMap(map);
        BoundValueOperations<String, Object> user = redisTemplate.boundValueOps(openid);
        user.set(xstWxUsers);
        if (count > 0) {
            return count;
        }else {
            return count;
        }
    }




    /**
     * 修改用户信息lw
     * @param form
     * @return
     */
    @PostMapping("/updateUserInfo")
    public String updateUserInfo(@RequestBody JSONObject form){
        System.out.println("jjajajaj:"+form);
        Integer integer = xstWxUserService.updateUserInfo(form);
        System.out.println("integer:"+integer);
        return null;
    }

    /**
     * 修改用户信息cg
     * @param form
     * @return
     */
    @PostMapping("/updateUserInfocg")
    public String updateUserInfocg(@RequestBody JSONObject form){
        System.out.println("jjajajaj:"+form);
        Integer integer = xstWxUserService.updateUserInfocg(form);
        System.out.println("integer:"+integer);
        return null;
    }



    @GetMapping("/selectUserInfo")
    public XstWxUser selectUserInfo(String userId){
        XstWxUser xstWxUser = xstWxUserDao.selectById(userId);
        return xstWxUser;
    }

    /**
     * 获取根据id获取用户信息
     * @param userId
     * @return
     */
    @GetMapping("/getUserInfoByid")
    public Map  getUserInfoByid(Integer userId){
        HashMap<String, Object> map = new HashMap<>();
        XstWxUser xstWxUser = xstWxUserDao.selectById(userId);
        String string = xstWxUser.getSchoolArea();
        String[] split = null;
        if(string !=null){
            String substring = string.substring(0, string.length() - 0);
            System.out.println(substring);
            split = substring.split(",");
//            for (String string2 : split) {
//                System.out.println("数据-->>>" + string2);
//            }
            map.put("states", 1);
            map.put("area", split);
            map.put("msg", "成功查询到用户");
            map.put("data", xstWxUser);
        } else {
            map.put("states", 0);
            map.put("msg", "用户不存在");
        }
        return map;
    }



}

